package com.supertange.miniapp.wepet.modules.wepet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.supertange.miniapp.wepet.common.idworker.Sid;
import com.supertange.miniapp.wepet.modules.wepet.entity.*;
import com.supertange.miniapp.wepet.modules.wepet.entity.vo.CommentsVO;
import com.supertange.miniapp.wepet.modules.wepet.entity.vo.UsersFansVO;
import com.supertange.miniapp.wepet.modules.wepet.entity.vo.UsersInfoVO;
import com.supertange.miniapp.wepet.modules.wepet.mapper.*;
import com.supertange.miniapp.wepet.modules.wepet.service.ExchangeService;
import com.supertange.miniapp.wepet.modules.wepet.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UsersMapper userMapper;

    @Autowired
    private UsersFansMapper usersFansMapper;

    @Autowired
    private UsersLikeVideosMapper usersLikeVideosMapper;

    @Autowired
    private UsersReportMapper usersReportMapper;

    @Autowired
    private CommentsMapperCustom commentsMapperCustom;

    @Autowired
    private ExchangeService exchangeService;

    @Autowired
    private Sid sid;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public boolean queryUsernameIsExist(String username) {

        UsersEntity result = userMapper.selectOne(new QueryWrapper<UsersEntity>().eq("username",username));

        return result == null ? false : true;
    }

    @Override
    public boolean queryNicknameIsExist(String nickname) {
        UsersEntity result = userMapper.selectOne(new QueryWrapper<UsersEntity>().eq("nickname",nickname));

        return result == null ? false : true;
    }

    @Override
    public UsersEntity queryNicknameByOpenId(String openId){
        UsersEntity result = userMapper.selectOne(new QueryWrapper<UsersEntity>().eq("openid",openId));

        return result;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void saveUser(UsersEntity user) {

        String userId = sid.nextShort();
        user.setId("user"+userId);
        userMapper.insert(user);
        exchangeService.addUser(user.getId());
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public UsersEntity queryUserForLogin(String username, String password) {

        UsersEntity result = userMapper.selectOne(new QueryWrapper<UsersEntity>()
                .eq("username", username)
                .eq("password", password));

        return result;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public UsersEntity queryUserInfo(String userId) {
        UsersEntity usersEntity = userMapper.selectById(userId);
        return usersEntity;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public boolean isUserLikeVideo(String userId, String videoId) {

        if (StringUtils.isBlank(userId) || StringUtils.isBlank(videoId)) {
            return false;
        }

        List<UsersLikeVideoEntity> list = usersLikeVideosMapper.selectList(new QueryWrapper<UsersLikeVideoEntity>()
                .eq("user_id", userId)
                .eq("video_id", videoId));

        if (list != null && list.size() > 0) {
            return true;
        }

        return false;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void saveUserFanRelation(String userId, String fanId) {

        String relId = sid.nextShort();

        UsersFansEntity userFan = new UsersFansEntity();
        userFan.setId(relId);
        userFan.setUserId(userId);
        userFan.setFanId(fanId);

        usersFansMapper.insert(userFan);

        userMapper.addFansCount(userId);
        userMapper.addFollowsCount(fanId);

    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void deleteUserFanRelation(String userId, String fanId) {
        usersFansMapper.delete(new QueryWrapper<UsersFansEntity>()
                .eq("user_id", userId)
                .eq("fan_id", fanId));

        userMapper.reduceFansCount(userId);
        userMapper.reduceFollowsCount(fanId);

    }

    @Override
    public boolean queryIfFollow(String userId, String fanId) {

        List<UsersFansEntity> list = usersFansMapper.selectList(new QueryWrapper<UsersFansEntity>()
                .eq("user_id", userId)
                .eq("fan_id", fanId));

        if (list != null && !list.isEmpty() && list.size() > 0) {
            return true;
        }

        return false;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void reportUser(UsersReport userReport) {

        String urId = sid.nextShort();
        userReport.setId(urId);

        usersReportMapper.insert(userReport);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int updateUserInfo(UsersInfoVO usersInfoVO) {
        UsersEntity usersEntity =new UsersEntity();
        BeanUtils.copyProperties(usersInfoVO, usersEntity);
        return userMapper.updateById(usersEntity);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<CommentsVO> getAllComments(String userId, Page page) {
        List<CommentsVO> pageList=commentsMapperCustom.queryMyComments(userId);
        return pageList;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public UsersEntity queryOpenidIsExist(String code) {
        UsersEntity usersEntity =userMapper.selectOne(new QueryWrapper<UsersEntity>().eq("openid",code));
        return usersEntity;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<UsersFansVO> queryMyFans(String userId,Page page) {
        List<UsersFansVO> usersFansList=usersFansMapper.queryUsersFansInfo(userId);
        return usersFansList;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<UsersFansVO> queryMyFollow(String userId,Page page) {
        List<UsersFansVO> usersFansList=usersFansMapper.queryUsersFollowInfo(userId);
        return usersFansList;
    }


}

